package com.yunyao.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.common.dto.SupplierSettlementPayNodeDto;
import com.yunyao.common.web.enums.ResultMsgEnum;
import com.yunyao.common.web.util.PageResult;
import com.yunyao.common.web.util.YResult;
import com.yunyao.dao.model.BamuSoSupply;
import com.yunyao.dao.model.BamuSpReconciliationOrderMaterial;
import com.yunyao.dao.model.BamuSpReconciliationSettlement;
import com.yunyao.dao.model.BamuSpReconciliationSettlementPayNode;
import com.yunyao.dao.service.bamu.IBamuSpReconciliationSettlementPayNodeService;
import com.yunyao.framework.dto.supplier.SupplierSettlementPayNodeSaveDto;
import com.yunyao.framework.service.SupplierSettlementPayNodeService;
import com.yunyao.framework.service.SupplierSettlementService;
import com.yunyao.framework.vo.spreconciliation.SpReconciliationMaterialVO;
import com.yunyao.framework.vo.supplier.SupplierSettlementInfoVO;
import com.yunyao.framework.vo.supplier.SupplierSettlementPayNodeInfoVO;
import com.yunyao.framework.vo.supplier.SupplierSettlementPayNodeVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SupplierSettlementPayNodeServiceImpl implements SupplierSettlementPayNodeService {
    @Autowired
    private IBamuSpReconciliationSettlementPayNodeService iBamuSpReconciliationSettlementPayNodeService;

    @Override
    public YResult<PageResult<SupplierSettlementPayNodeVO>> pageList(SupplierSettlementPayNodeDto supplierSettlementPayNodeDto) {
        LambdaQueryWrapper<BamuSpReconciliationSettlementPayNode> queryWrapper =
                new LambdaQueryWrapper<BamuSpReconciliationSettlementPayNode>()
                        .eq(StringUtils.isNotBlank(supplierSettlementPayNodeDto.getSettlementId()), BamuSpReconciliationSettlementPayNode::getSettlementId, supplierSettlementPayNodeDto.getSettlementId())
                        .eq(StringUtils.isNotBlank(supplierSettlementPayNodeDto.getContractId()), BamuSpReconciliationSettlementPayNode::getContractId, supplierSettlementPayNodeDto.getContractId())
//                        .eq(StringUtils.isNotBlank(supplierSettlementPayNodeDto.getProjectId()), BamuSpReconciliationSettlementPayNode::getProjectId, supplierSettlementPayNodeDto.getProjectId())
                        .eq(BamuSpReconciliationSettlementPayNode::getIsDeleted, DataDeletedStatus.NO.getType());

        // 分页查询
        IPage<BamuSpReconciliationSettlementPayNode> payPage = new Page<>(supplierSettlementPayNodeDto.getPageIndex(), supplierSettlementPayNodeDto.getPageSize());
        payPage = iBamuSpReconciliationSettlementPayNodeService.page(payPage, queryWrapper);

        // 处理查询结果
        if (CollectionUtils.isEmpty(payPage.getRecords())) {
            return YResult.success(new PageResult<>());
        }

        // 转换为VO对象列表
        List<SupplierSettlementPayNodeVO> materialVOList = materialDaoToVoList1(payPage.getRecords());

        // 返回分页结果
        return YResult.success(new PageResult<>(materialVOList, payPage.getCurrent(), payPage.getSize(), payPage.getPages(), payPage.getTotal()));

    }

    @Override
    public YResult<SupplierSettlementPayNodeInfoVO> getInfo(SupplierSettlementPayNodeDto supplierSettlementPayNodeDto) {
        // 创建查询条件，用于筛选未被删除且ID匹配的结算记录
        LambdaQueryWrapper<BamuSpReconciliationSettlementPayNode> queryWrapper =
                new LambdaQueryWrapper<BamuSpReconciliationSettlementPayNode>()
                        .eq(BamuSpReconciliationSettlementPayNode::getIsDeleted,
                                DataDeletedStatus.NO.getType()).eq(BamuSpReconciliationSettlementPayNode::getId, supplierSettlementPayNodeDto.getId());

        // 使用查询条件获取第一个匹配的结算记录
        BamuSpReconciliationSettlementPayNode bamuSpReconciliationSettlement =
                iBamuSpReconciliationSettlementPayNodeService.getOne(queryWrapper);

        // 如果结算记录为空，返回错误结果，表示没有数据
        if (Objects.isNull(bamuSpReconciliationSettlement)) {
            return YResult.success();
        }

        // 将获取到的结算记录转换为供应商结算信息VO，并返回成功结果
        return YResult.success(daoToInfoVo(bamuSpReconciliationSettlement));
    }

    @Override
    public YResult<String> saveInfo(SupplierSettlementPayNodeSaveDto saveDto) {
        BamuSpReconciliationSettlementPayNode bamuSoSupply = new BamuSpReconciliationSettlementPayNode();
        BeanUtils.copyProperties(saveDto, bamuSoSupply);
        iBamuSpReconciliationSettlementPayNodeService.saveOrUpdate(bamuSoSupply);
        return YResult.success(bamuSoSupply.getId());
    }

    private SupplierSettlementPayNodeInfoVO daoToInfoVo(BamuSpReconciliationSettlementPayNode bamuSpReconciliationSettlement) {
        SupplierSettlementPayNodeInfoVO supplierSettlementInfoVO = new SupplierSettlementPayNodeInfoVO();
        BeanUtils.copyProperties(bamuSpReconciliationSettlement, supplierSettlementInfoVO);
        return supplierSettlementInfoVO;
    }
    private List<SupplierSettlementPayNodeVO> materialDaoToVoList1(List<BamuSpReconciliationSettlementPayNode> records) {
        return records.stream().map(this::materialDaoToVo1).collect(Collectors.toList());
    }

    private SupplierSettlementPayNodeVO materialDaoToVo1(BamuSpReconciliationSettlementPayNode bamuSpReconciliationOrderMaterial) {
        SupplierSettlementPayNodeVO spReconciliationMaterialVO = new SupplierSettlementPayNodeVO();
        BeanUtils.copyProperties(bamuSpReconciliationOrderMaterial, spReconciliationMaterialVO);
        return spReconciliationMaterialVO;
    }
}
